import { Component } from 'react'
import React from 'react'
import ReactDOM from 'react-dom'


class App extends Component {
  txtRef = React.createRef()
  state = {
    username: 'Hello',
    msg2: '',
    city: 3,
    agree: false,
    // 非组件控件
  }
  render() {
    return <div>
      <h1>非受控组件</h1>
      <input type="text" ref={this.txtRef} />
      <button onClick={this.clickF}>非受控组件</button>
      <hr />
      <h1>受控组件</h1>
      {/* input框通过value值控制 */}
      <input type="text"
        onChange={this.handleChange}
        value={this.state.username}
        name="username"
      />
      <h3>文本域</h3>
      <textarea
        value={this.state.msg2}
        onChange={this.handleChange}
        name="msg2"
        id=""
        cols="30"
        rows="10">
      </textarea>
      <h3>下拉框</h3>
      <select name="city" onChange={this.handleChange} value={this.state.city}>
        <option value="1">北京</option>
        <option value="2">上海</option>
        <option value="3">武汉</option>
      </select>
      <div>
        <label htmlFor="y">是否同意我们的协议</label>
        <input name='agree' onChange={this.handleChange} checked={this.state.agree} id='y' type="checkbox" />
      </div>

    </div>
  }
  handleChange = (e) => {
    const { name, type, checked, value } = e.target
    console.log(name);
    this.setState({
      [name]: type === 'checkbox' ? checked : value
    })
  }
  clickF = () => {
    console.log(this.txtRef.current.value);
  }
}

ReactDOM.render(<App></App>, document.getElementById('root'))